﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DAL;
using WCFMINERP.MODEL;

namespace WCFMINERP.BLL.PhotoDownload
{
    public class PhotoDownloadBll
    {
        /// <summary>
        /// 获取photoInfo（产品图片表）表的数据，将它添加到T_PhotoDownload（图片下载表）表里面去
        /// </summary>
        public int implementMethod()
        {
            try
            {
                int Number = 0;
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    int max = 0;
                    if (db.T_PhotoDownload != null)
                    {
                        if (db.T_PhotoDownload.Count() > 0)
                        {
                            max = db.T_PhotoDownload.Max(p1 => p1.Photoid);
                        }
                        //WriteAnalyseLog.WriteLogFile(DateTime.Now + ":" + max);
                    }
                    List<photoInfo> listPhotoInfo = db.photoInfo.Where(p => p.photoId > max).ToList();
                    List<Tb_OutShop_ProductCategory> outshopList = db.Tb_OutShop_ProductCategory.ToList();
                    foreach (photoInfo photo in listPhotoInfo)
                    {
                        if (!photo.orderInfoReference.IsLoaded)
                        {
                            photo.orderInfoReference.Load();
                        }
                        if (!photo.orderInfo.Tb_ProductReference.IsLoaded)
                        {
                            photo.orderInfo.Tb_ProductReference.Load();
                        }
                        int productid = photo.orderInfo.Tb_Product.ProuctId;

                        List<Tb_OutShop_ProductCategory> productidList = outshopList.Where(p => p.ProductId == productid).ToList();

                        foreach (Tb_OutShop_ProductCategory outshop in productidList)
                        {
                            T_PhotoDownload model = new T_PhotoDownload();
                            model.Photoid = photo.photoId;
                            model.OrderProductid = photo.orderInfo.OrderProductId;
                            model.State = 0;
                            model.OutShopId = outshop.OutShopId;
                            model.CreateDateTime = DateTime.Now;
                            model.FileType = 0;
                            model.PicPath = photo.picPath;
                            db.T_PhotoDownload.AddObject(model);
                            Number++;
                            db.SaveChanges();
                        }
                    }

                }
                return Number;
            }
            catch (Exception ex)
            {                
                WriteAnalyseLog.WriteLogFile(DateTime.Now + ":"+ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// 获取下载数据（只获取FileType=1)
        /// </summary>
        /// <returns></returns>
        public List<CusFileInfo> getFileInfoList(DateTime StartTime,DateTime endTime)
        {            
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<OutProcessShop> outshopList = db.OutProcessShop.ToList();
                List<CusFileInfo> listInfo = new List<CusFileInfo>();
                List<T_PhotoDownload> oldList = db.T_PhotoDownload.Where(p => p.FileType == 1 && p.CreateDateTime >= StartTime && p.CreateDateTime <= endTime).ToList();
                foreach (T_PhotoDownload item in oldList)
                {
                    CusFileInfo info = new CusFileInfo();
                    info.filepath = item.PicPath;
                    info.check = false;
                    info.downLoadid = item.Id;
                    info.createtime = item.CreateDateTime;
                    info.outshopname = outshopList.FirstOrDefault(p => p.OutShopId == item.OutShopId).OutShopName;
                    info.outshopid = item.OutShopId;
                    listInfo.Add(info);
                }
                return listInfo;
            }
        }

        /// <summary>
        /// 删除系统时间前3天以前的数据
        /// </summary>
        /// <returns></returns>
        public int deleteDownData()
        {
            int Number = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                DateTime date = DateTime.Now.AddDays(-3);
                if (db.T_PhotoDownload!=null)
                {
                    return 0;
                }
                List<T_PhotoDownload> listData = db.T_PhotoDownload.Where(p => p.CreateDateTime < date && p.FileType==0).ToList();  //只删除订单的，不删除文件的
                foreach (T_PhotoDownload item in listData)
                {
                    db.T_PhotoDownload.DeleteObject(item);
                    Number++;
                    db.SaveChanges();
                }
            }
            return Number;
        }

/// <summary>
/// 删除下载的数据
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
        public bool DeleteDownData(string filepath,int oupshopid)
        {
            bool b = true;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                List<T_PhotoDownload> listData = db.T_PhotoDownload.Where(p => p.PicPath == filepath && p.OutShopId == oupshopid).ToList();  //只删除订单的，不删除文件的
                foreach (T_PhotoDownload item in listData)
                {
                    db.T_PhotoDownload.DeleteObject(item);                  
                     int id=db.SaveChanges();
                     if (id == 0)
                     {
                         b = false;
                         break;
                     }
                }
            }
            return b;
        }

      /// <summary>
        /// 添加文件夹里的下载
      /// </summary>
      /// <param name="shopidlist">所选工厂集合</param>
      /// <param name="picpath">文件路径</param>
      /// <returns></returns>
        public bool AddDownLoad(List<OutShop> shopidlist, string picpath)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                bool b = true;
                foreach (OutShop item in shopidlist)
                {
                    T_PhotoDownload model = new T_PhotoDownload();
                    model.Photoid = 0;
                    model.OrderProductid = 0;
                    model.State = 0;  //未下载
                    model.OutShopId = item.outshopid;
                    model.CreateDateTime = DateTime.Now;
                    model.FileType = 1;
                    model.PicPath = picpath;
                    db.T_PhotoDownload.AddObject(model);
                    int returnid = db.SaveChanges();
                    if (returnid > 0)
                    {

                    }
                    else
                    {
                        b = false;
                        break;
                    }
                }
                return b;
            }
        }

        /// <summary>
        /// 获取所有的
        /// </summary>
        /// <returns></returns>
        public List<OutShop> GetShopList()
        {
            List<OutShop> list = new List<OutShop>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<OutProcessShop> outshopList = db.OutProcessShop.ToList();
                foreach (OutProcessShop item in outshopList)
                {
                    OutShop model = new OutShop();
                    model.outshopid = item.OutShopId;
                    model.shopName = item.OutShopName;

                    list.Add(model);
                }
            }
            return list;
        }
    }

    public class OutShop
    {
        public int outshopid { get; set; }

        public string shopName { get; set; }
    }
}
